//
//  DDCompanyMicroHeadView.m
//  DingDongZP
//
//  Created by Ray on 16/5/4.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDCompanyMicroHeadView.h"
#import "CCUICreator.h"
#import "DDCompanyInfo.h"
#import "DDAPIGenerate.h"

@interface DDCompanyMicroHeadView (){
    
    NSArray *companyShow;
    UILabel *number;
    
}

@property (nonatomic, strong) UIScrollView *imgScroll;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *shortNameLab;
@property (nonatomic, strong) UILabel *websiteLab;
@property (nonatomic, strong) UILabel *scopeLab;
@property (nonatomic, strong) UILabel *financeLab;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) UILabel *fullNameLab;
@property (nonatomic, strong) UILabel *addressLab;
@property (nonatomic, strong) UILabel *pageLab;

@property (nonatomic, strong) DDCompanyInfo *compInfo;
@property (nonatomic, strong) DDBossInfo *bossInfo;
@property (nonatomic ,assign) BOOL isDragging;
@property (nonatomic ,assign) NSInteger count;


@end

@implementation DDCompanyMicroHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.imgScroll = [[UIScrollView alloc]init];
        self.imgScroll.pagingEnabled = YES;
        self.imgScroll.backgroundColor = COLOR_NAVIGATIONBAR;
        [self addSubview:self.imgScroll];
        [self.imgScroll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.width.equalTo(self);
            make.height.mas_equalTo(SCREEN_WIDTH*0.58);
        }];
        
        self.iconView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self addSubview:_iconView];
        [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(70, 70));
            make.centerX.equalTo(self.imgScroll);
            make.centerY.equalTo(self.imgScroll.mas_bottom);
        }];
        self.iconView.layer.cornerRadius = 35;
        self.iconView.layer.masksToBounds = YES;
        
        self.pageLab = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-40, 185*[UIScreen mainScreen].bounds.size.height/667, 10, 25)];
        self.pageLab.font = [UIFont systemFontOfSize:17.0];
        self.pageLab.textColor = [UIColor whiteColor];
        [self addSubview:self.pageLab];
//        [self.pageLab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.trailing.equalTo(self.imgScroll);
//            make.bottom.equalTo(self.imgScroll);
//        }];
        
        UILabel *shortName = [[UILabel alloc] initWithFrame:CGRectZero];
        shortName.font = [UIFont systemFontOfSize:17.0];
        shortName.textColor = COLOR_FONT_MAIN_GREEN;
        shortName.text = @"";
        [self addSubview:shortName];
        [shortName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconView.mas_bottom).offset(10);
            make.centerX.equalTo(self);
        }];
        self.shortNameLab = shortName;
        
        UILabel *website = [UILabel labelWithFrame:CGRectZero font:[UIFont systemFontOfSize:14.0] textColor:COLOR_FONT_LIGHT_GRAY text:nil];
        [self addSubview:website];
        [website mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(shortName);
            make.top.equalTo(shortName.mas_bottom).offset(5);
        }];
        self.websiteLab = website;
        
        UILabel *scope = [UILabel labelWithFrame:CGRectZero font:[UIFont systemFontOfSize:15.0] textColor:COLOR_FONT_BLACK text:nil];
        [self addSubview:scope];
        [scope mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_right).dividedBy(6);
            make.top.equalTo(self.websiteLab.mas_bottom).offset(5);
        }];
        self.scopeLab = scope;
        
        
        UILabel *finance = [UILabel labelWithFrame:CGRectZero font:[UIFont systemFontOfSize:15.0] textColor:COLOR_FONT_BLACK text:nil];
        [self addSubview:finance];
        [finance mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_right).dividedBy(2);
            make.top.equalTo(self.websiteLab.mas_bottom).offset(5);
        }];
        self.financeLab = finance;
        
        UILabel *count = [UILabel labelWithFrame:CGRectZero font:[UIFont systemFontOfSize:15.0] textColor:COLOR_FONT_BLACK text:nil];
        [self addSubview:count];
        [count mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_right).multipliedBy(5/6.0);
            make.top.equalTo(self.websiteLab.mas_bottom).offset(5);
        }];
        self.countLab = count;
        
        UILabel *fullName = [UILabel labelWithFrame:CGRectZero font:[UIFont systemFontOfSize:15.0] textColor:COLOR_FONT_BLACK text:nil];
        [self addSubview:fullName];
        [fullName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.financeLab.mas_bottom).offset(10);
            make.centerX.equalTo(self);
        }];
        self.fullNameLab = fullName;
        
        UILabel *address = [UILabel labelWithFrame:CGRectZero font:[UIFont systemFontOfSize:15.0] textColor:COLOR_FONT_BLACK text:nil];
        [self addSubview:address];
        [address mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(fullName.mas_bottom).offset(10);
            make.centerX.equalTo(self);
            make.width.lessThanOrEqualTo(self);
        }];
        self.addressLab = address;
    }
    return self;
}

- (void)setCompanyInfo:(DDCompanyInfo *)compInfo {
    
    _compInfo = compInfo;
    
    companyShow = compInfo.companyshow;
    /**
     *  设置默认图
     */
    if (companyShow.count == 0) {
        
        UIImageView *ban = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.imgScroll.frame.size.width, self.imgScroll.frame.size.height)];
        ban.image = [UIImage imageNamed:@"timeline_image_loading"];
        [self.imgScroll addSubview:ban];
        self.pageLab.text = @"0";
    }else{
    /// 设轮播图
    for (int i = 0; i < companyShow.count; i++) {
        
        DDCompanyshow *show = companyShow[i];
        
        NSString *url = [DDAPIGenerate imageURLWithPath:show.show_logo];
        
        UIImageView *ban = [[UIImageView alloc] initWithFrame:CGRectMake(self.imgScroll.frame.size.width*i, 0, self.imgScroll.frame.size.width, self.imgScroll.frame.size.height)];
        [ban sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil];
        //            ban.image = [UIImage imageNamed:@"common_avatar"];
        [self.imgScroll addSubview:ban];
        self.imgScroll.scrollEnabled = NO;
    }
    self.imgScroll.contentSize = CGSizeMake(self.imgScroll.frame.size.width * companyShow.count, self.imgScroll.size.height);
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(rotain) userInfo:nil repeats:YES];
    self.pageLab.text = @"1";

  }
    number = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-30, 185*[UIScreen mainScreen].bounds.size.height/667, 20, 25)];
    number.textColor = [UIColor whiteColor];
    number.font = [UIFont systemFontOfSize:17.0];
    number.text = [NSString stringWithFormat:@"/%ld",companyShow.count];
    [self addSubview:number];

    // 公司logo
    NSString *iconurl = [DDAPIGenerate imageURLWithPath:compInfo.company_logo];
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:iconurl] placeholderImage:[UIImage imageNamed:@"default_header"]];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    for (NSInteger i = 5; i < companyShow.count; i++) {
        NSInteger y = self.imgScroll.contentOffset.x/self.imgScroll.frame.size.width;
        self.pageLab.text = [NSString stringWithFormat:@"%ld",y];
    }
    
}

- (void)rotain
{
    self.pageLab.text =[NSString stringWithFormat:@"%.0f", self.imgScroll.contentOffset.x/self.imgScroll.frame.size.width+2];
    if (self.imgScroll.contentOffset.x/self.imgScroll.frame.size.width == companyShow.count-1) {
        self.pageLab.text = @"1";
    }
    if (companyShow.count == 0) {
        self.pageLab.text = @"0";
    }

    [self.imgScroll setContentOffset:CGPointMake(self.imgScroll.contentOffset.x + [UIScreen mainScreen].bounds.size.width, 0 ) animated:YES];
    if (self.imgScroll.contentOffset.x == (companyShow.count - 1 ) * [UIScreen mainScreen].bounds.size.width ) {
        [self.imgScroll setContentOffset:CGPointMake(0 , 0) animated:NO];
    }
}

- (void)setBossInfo:(DDBossInfo *)bossInfo {
    
    _bossInfo = bossInfo;
    
    /// short name
    self.shortNameLab.text = bossInfo.companyshortname;
    
    /// website
    self.websiteLab.text = bossInfo.companywebsite;
    /// infos, industry, finance, staff
    self.scopeLab.attributedText = [self string:bossInfo.industry withIcon:[UIImage imageNamed:@"icon_industry"]];
    self.financeLab.attributedText = [self string:bossInfo.companyfinancing withIcon:[UIImage imageNamed:@"icon_finance"]];
    self.countLab.attributedText = [self string:bossInfo.companysize withIcon:[UIImage imageNamed:@"icon_staff"]];
    
    /// full name and address
    self.fullNameLab.text = bossInfo.companyfullname;
    self.addressLab.text = bossInfo.companyaddress;
}

- (NSAttributedString *)string:(NSString *)string withIcon:(UIImage *)icon {
    if (string == nil || icon == nil) {
        return [NSAttributedString new];
    }
    NSMutableAttributedString *titleAttributed = [[NSMutableAttributedString alloc] initWithString:string];
    [titleAttributed addAttribute:NSForegroundColorAttributeName
                            value:COLOR_FONT_DARK_GRAY
                            range:NSMakeRange(0,string.length)];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment.image = icon;
    
    textAttachment.bounds = CGRectMake(0, 0, icon.size.width, icon.size.height);
    [titleAttributed insertAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment] atIndex:0];
    [titleAttributed insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:1];
    return titleAttributed;
}
@end
